package com.quanyan.stadium.service.gameRules.impl;

import com.quanyan.stadium.biz.BizGameSystemDao;
import com.quanyan.stadium.entity.TbGameEvent;
import com.quanyan.stadium.entity.TbGameRound;
import com.quanyan.stadium.entity.vo.GameGroup;
import com.quanyan.stadium.mapper.TbGameScheduleAddendumMapper;
import com.quanyan.stadium.service.gameRules.AbsGameSystem;
import com.quanyan.stadium.service.gameRules.entity.GameRuleGenerate;
import com.quanyan.stadium.service.gameRules.GameSystemService;
import com.quanyan.stadium.utils.GameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoxiao
 * @date 16/4/11-14:42
 *
 * 等位制设置
 *
 */

@Service("gameRuleAllelicServiceImpl")
public class GameRuleAllelicServiceImpl extends AbsGameSystem {

    private final static Logger logger = LoggerFactory.getLogger(GameRuleAllelicServiceImpl.class);

    @Autowired
    private GameCategoryServiceImpl gameCategoryService;

    @Autowired
    private BizGameSystemDao bizGameSystemDao;

    @Autowired
    private GameSystemService gameSystemService;

    @Autowired
    private TbGameScheduleAddendumMapper tbGameScheduleAddendumMapper;

    /**
     * params中多存在 TbGameRound 对象
     *
     * */
    @Override
    public List<Integer> createGameRound(Integer gameEventId, Integer currentTeamNum, Map<String, Object> params) throws Exception {
        TbGameEvent tbGameEvent = (TbGameEvent) params.get("tbGameEvent");
        currentTeamNum = ((List<Integer>)params.get("teamIds")).size();
        int roundNum = GameUtils.washedRoundleNum(currentTeamNum.doubleValue());
        List<Integer> result = new ArrayList<>();
        for (int i=0; i < roundNum; i++) {
            TbGameRound gameRound = new TbGameRound();
            gameRound.setCurrentTeamNum(currentTeamNum);
            gameRound.setRankTeamNum(currentTeamNum);
            gameRound.setRoundStatus((byte) (i+1));
            gameRound.setGameEventId(tbGameEvent.getId());
            if (null == bizGameSystemDao.insertTbGameRound(gameRound).getId()) {
                throw new Exception("数据库异常");
            }
            result.add(gameRound.getId());
        }

        List<TbGameRound> tbGameRoundList = bizGameSystemDao.selectTbGameRoundByGameEventId(gameEventId);
        //更新赛事进行中的轮次
        TbGameEvent event = new TbGameEvent();
        event.setId(gameEventId);
        event.setCurrentRoundId(tbGameRoundList.get(0).getId());
        bizGameSystemDao.updateTbGameEvent(event);

        //生成当前第一轮赛程所有赛程
        params.put("tbGameRound", tbGameRoundList.get(0));
        initFirstGameRuleAllelic(params);

        return result;
    }


    public void initFirstGameRuleAllelic(Map<String, Object> params){
        List<Integer> teamIds = (List<Integer>) params.get("teamIds");
        TbGameRound tbGameRound = (TbGameRound) params.get("tbGameRound");
        Collections.shuffle(teamIds);   //等位制加入洗牌模式
        List<GameGroup> gameGroups = gameSystemService.swissGroupNew(teamIds, tbGameRound, null, true);
        this.generateScheduleAndUpdateRound(true, gameGroups, null);
    }




    @Override
    public List<Integer> createGameSchedule(TbGameEvent tbGameEvent, GameRuleGenerate gameRuleGenerate) {
        gameRuleGenerate.getTbTeamDoubleAddendumList();
        gameRuleGenerate.getTbGameEventTeamList();
        Map<String, Object> params = gameRuleGenerate.getParams();
        List<Integer> teamIds = (List<Integer>) params.get("teamIds");
        Integer preGameRoundId = (Integer) params.get("teamIds");
        TbGameRound tbGameRound = (TbGameRound) params.get("tbGameRound");
        gameSystemService.swissGroupNew(teamIds, tbGameRound, preGameRoundId, false);
        return null;
    }
}
